package com.zhongli.devplatform.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongli.devplatform.constant.Consts;
import com.zhongli.devplatform.mapper.user.ReadTeachClazzMapper;
import com.zhongli.devplatform.mapper.user.ReadUserInfoMapper;
import com.zhongli.devplatform.mapper.user.ReadUserParentMapper;
import com.zhongli.devplatform.po.common.ReadTeachClazzPO;
import com.zhongli.devplatform.po.user.ReadUserAttFansPO;
import com.zhongli.devplatform.po.user.ReadUserInfoPO;
import com.zhongli.devplatform.po.user.ReadUserParentPO;
import com.zhongli.devplatform.service.imp.common.ReadTeachClazzService;
import com.zhongli.devplatform.service.imp.user.ReadUserInfoService;
import com.zhongli.devplatform.service.imp.common.ReadUserAttFansService;
import com.zhongli.devplatform.vo.student.CurrentStudentInfoVO;
import com.zhongli.devplatform.bean.Res;
import com.zhongli.devplatform.vo.student.StudentAndParentInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author  wy
 * @since   2020/7/23 10:12
 * @version 1.0
 */
@Service
public class ReadUserInfoServiceImpl extends ServiceImpl<ReadUserInfoMapper, ReadUserInfoPO> implements ReadUserInfoService {

    @Autowired
    private ReadUserAttFansService userAttFansService;

    @Autowired
    private ReadUserInfoMapper userInfoMapper;

    @Autowired
    private ReadUserParentMapper userParentMapper;

    @Autowired
    private ZSetOperations<String,Object> zSetOperations;

    @Autowired
    private ReadTeachClazzMapper teachClazzMapper;

    /**
     * 每天凌晨
     * 统计粉丝数 和关注数
     */
    @Override
    public void statisticsPinkAttention() {
        List<ReadUserInfoPO> users = list();
        List<Integer> userIds = users.stream().map(ReadUserInfoPO::getUserId).collect(Collectors.toList());

        //修改关注数量  和粉丝数量
        userIds.forEach(id->{
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Date zero = calendar.getTime();
            RedisZSetCommands.Range range = new RedisZSetCommands.Range();
            range.gte(zero.getTime());
            Set<Object> fans = zSetOperations.rangeByLex(Consts.USER_FANS + id, range);
            if (fans.size() > 0) {
                for (Object fan : fans) {
                    ReadUserAttFansPO attFansPO = userAttFansService.getOne(new LambdaQueryWrapper<ReadUserAttFansPO>()
                            .eq(ReadUserAttFansPO::getFromUserId, fan)
                            .eq(ReadUserAttFansPO::getToUserId, id));
                    if (Objects.isNull(attFansPO)) {
                        ReadUserAttFansPO readUserAttFansPO = new ReadUserAttFansPO();
                        readUserAttFansPO.setFromUserId((Integer) fan);
                        readUserAttFansPO.setToUserId(id);
                        readUserAttFansPO.setCreateTime(new Date());
                        readUserAttFansPO.setCreateUserId((Integer) fan);
                        userAttFansService.save(readUserAttFansPO);
                    }
                }
            }
            //启用
            int attCount = userAttFansService.count(new QueryWrapper<ReadUserAttFansPO>().eq("from_user_id", id));
            int fansCount = userAttFansService.count(new QueryWrapper<ReadUserAttFansPO>().eq("to_user_id", id));
            ReadUserInfoPO userInfoPO = new ReadUserInfoPO();
            userInfoPO.setAttCount(attCount);
            userInfoPO.setFansCount(fansCount);
            update(userInfoPO,new UpdateWrapper<ReadUserInfoPO>().eq("user_id",id));
        });

    }

    @Override
    public Res getCurrentStudentInfo(Integer currentUserId) {
        //学生基本信息
        CurrentStudentInfoVO currentStudentInfo = userInfoMapper.getCurrentStudentInfo(currentUserId);
        List<ReadUserParentPO> userParentPOS = userParentMapper.selectList(new QueryWrapper<ReadUserParentPO>().eq("user_id", currentUserId));
        ReadTeachClazzPO teachClazzPO = teachClazzMapper.selectOne(new QueryWrapper<ReadTeachClazzPO>()
                .eq("clazz_id", currentStudentInfo.getClazzId())
                .eq("is_admin_teacher", true));
        if (!Objects.isNull(teachClazzPO)){
            //班主任信息
            ReadUserInfoPO userInfoPO = userInfoMapper.selectOne(new QueryWrapper<ReadUserInfoPO>().eq("user_id",teachClazzPO.getTeacherId()));
            if (!Objects.isNull(userInfoPO)){
                currentStudentInfo.setAdminTeacherName(userInfoPO.getNickName());
            }
        }
        //家长信息
        List<Integer> parentIds = userParentPOS.stream().map(ReadUserParentPO::getParentId).collect(Collectors.toList());
        if (parentIds.size() != 0){
            List<ReadUserInfoPO> parents = userInfoMapper.selectList(new QueryWrapper<ReadUserInfoPO>().in("user_id", parentIds));
            List<StudentAndParentInfoVO> list = new ArrayList<>();
            parents.forEach(item ->{
                StudentAndParentInfoVO parentInfoVO = new StudentAndParentInfoVO();
                parentInfoVO.setId(item.getUserId());
                parentInfoVO.setNickName(item.getNickName());
                parentInfoVO.setPhone(item.getPhone());
                list.add(parentInfoVO);
            });
            currentStudentInfo.setParents(list);
        }
        return Res.ok(currentStudentInfo);
    }

}
